﻿/**string:原始字符串
*substr:子字符串
*isIgnoreCase:忽略大小写*/
function StrContains(string, substr, isIgnoreCase)
{
    if (isIgnoreCase)
    {
        string = string.toLowerCase();
        substr = substr.toLowerCase();
    }
    var startChar = substr.substring(0, 1);
    var strLen = substr.length;
    for (var j = 0; j < string.length - strLen + 1; j++)
    {
        if (string.charAt(j) == startChar)//如果匹配起始字符,开始查找
        {
            if (string.substring(j, j + strLen) == substr)//如果从j开始的字符与str匹配，那ok
            {
                return true;
            }
        }
    }
    return false;
}

//动态加载脚本
//调用示例：
//var jsLoader = new JsLoader();
////定义载入成功处理程序
//jsLoader.onsuccess = function (){Checklogin();}
//jsLoader.load("ui/index.js");//开始载入
function JsLoader()
{
    this.load = function (url)
    {
        //获取所有的<script>标记
        var ss = document.getElementsByTagName("script");
        //判断指定的文件是否已经包含，如果已包含则触发onsuccess事件并返回
        for (i = 0; i < ss.length; i++)
        {
            if (ss[i].src && ss[i].src.indexOf(url) != -1)
            {
                this.onsuccess();
                return;
            }
        }
        //创建script结点,并将其属性设为外联JavaScript文件
        s = document.createElement("script");
        s.type = "text/javascript";
        s.src = url;
        //获取head结点，并将<script>插入到其中
        var head = document.getElementsByTagName("head")[0];
        head.appendChild(s);

        //获取自身的引用
        var self = this;
        //对于IE浏览器，使用readystatechange事件判断是否载入成功
        //对于其他浏览器，使用onload事件判断载入是否成功
        //s.onload=s.onreadystatechange=function(){
        s.onload = s.onreadystatechange = function ()
        {
            //在此函数中this指针指的是s结点对象，而不是JsLoader实例,
            //所以必须用self来调用onsuccess事件，下同。
            if (this.readyState && this.readyState == "loading") return;
            self.onsuccess();
        }
        s.onerror = function ()
        {
            head.removeChild(s);
            self.onfailure();
        }
    };
    //定义载入成功事件
    this.onsuccess = function () { };
    //定义失败事件
    this.onfailure = function () { };
}

function Checklogin()
{
    if (getCookie("savename") != null && getCookie("savename") != "" && getCookie("savepass") != null && getCookie("savepass") != "")
    {
        var name = getCookie("savename");
        var pass = getCookie("savepass");
        $('#username').val(name);
        $('#password').val(pass);
    }
}
function login()
{
    var name = $('#username').val();
    var pass = $('#password').val();
    if (name == "" || pass == "") {
        alert("【账号】或【密码】不能为空!");
    }
    else {            
        jq.ui.showMask("正在登陆...");
        if (tempObj == "z") {
            $.post('ProcTeacher/login.ashx', { uname: name, upass: pass }, function (data) { loginHome1(data); });
        } else {
            $.post('Proc/login.ashx', { uname: name, upass: pass }, function (data) { loginHome(data, name, pass, "0"); });
        } 
    }
}

function loginHome1(data) {
    if (data == "0") {
        alert("账号或密码错误！");
    }
    else {
        if (document.getElementById("css3").checked)
            SetCookie("savepass", data, 30);
        else
            deleteCookie("savepass"); alert(data);
        window.location.href = "Default.aspx?SID=" + data;
    }
}
function loginHome(data, u, p, s)
{
    if (data == "N")
        alert("账号或密码错误！");
    else if (data == "P")
        alert("账号已过期！");
    else if (data == "1")
    {
        $('#password').val("");
        $.ui.loadContent('webhome', false, false, 'slide');
        $("#puserinfo").html("当前登陆名：" + $('#username').val());

        if (document.getElementById("css3").checked)
        {
            SetCookie("savename", u, 7);
            SetCookie("savepass", p, 7);
        }
        else
        {
            deleteCookie("savename");
            deleteCookie("savepass");
        }
    }
    else
        alert("未知错误，请确保网络畅通后登陆！");
}
function cancel()
{
    $('#username').val("");
    $('#password').val("");
}
function SetCookie(name, value, Days)//三个参数，一个是cookie的名子，一个是值,一个过期天数
{
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}
function deleteCookie(name)
{
    if (getCookie(name) == null)
        return;
    SetCookie(name, "", 0);
}
var queryString = function (key)
{
    return (document.location.search.match(new RegExp("(?:^\\?|&)" + key + "=(.*?)(?=&|$)")) || ['', null])[1];
}

function isonloaded(obj)
{
    if (obj == "@@N*L@@")
    {
        $.ui.loadContent('firstpage', false, false, 'slide');
        return false;
    }
}

var formname = "";
//获取待办事宜数量
function GetTaskCount()
{
    $.post('Proc/GetData.ashx', { TYPE: "GetTaskCount", PARA: "" }, function (data)
    {
        isonloaded(data);
        $.ui.updateBadge("#litaskcount", data);
    });

}

function unload()
{
    jq.ui.showMask("正在注销...");
    $.post('Proc/GetData.ashx', { TYPE: "UnLoad", PARA: "" }, function (data)
    {
        $.ui.loadContent('firstpage', false, false, 'slide');
        hasloadedtask = "0";
    });

}
var tempObj;
function loadsys(obj) {   
    tempObj = obj;
    if (obj === "m" || obj === "z")
    {
        $.ui.loadContent('webloading', false, false, 'slide');
    }
    else
    {
        window.location.href = "../Login.aspx?Comefrom=1";
    }
}

var hasloadedtask = "0";
function LoadDaiban(str)
{
    jq.ui.showMask("正在加载...");
    var formnames = "";
    if (str == 1)
    {
        formnames = formname;
    }
    else
    {
        formname = "";
    }

    if (hasloadedtask=="0" || str == 1)
    {
        $.ajax({
            type: 'POST',
            url: 'Proc/GetData.ashx',
            headers: {},
            dataType: 'html',
            data: { TYPE: "GetTask", PARA: "0︾20" + "︾" + formnames },
            success: function (data)
            {
                isonloaded(data);
                $($("#webchildid").siblings()).remove();
                document.getElementById("daibanyeshu").value = 1;
                $("#webchildid").parent().get().innerHTML += CreateTaskHtml(data, 0);
                hasloadedtask = "1";
                $.ui.loadContent('webslider', false, false, 'slide');
                $.ui.scrollToTop('webslider');
                jq.ui.hideMask();
                GetTaskCount();

            },
            error: function (err) { alert("网络不可用或账号信息过时，请重新登录"); }
        });
    }
    else
    {
        $.ui.loadContent('webslider', false, false, 'slide');
        jq.ui.hideMask();
    }
}

function freshpage()
{
    if ($("#webchildid").siblings().length < 20)
        return;
    jq.ui.showMask("正在加载...");
    var num = document.getElementById("daibanyeshu");
    var formnames = formname;
    $.ajax({
        type: 'POST',
        url: 'Proc/GetData.ashx',
        headers: {},
        dataType: 'html',
        data: { TYPE: "GetTask", PARA: num.value + "︾20" + "︾" + formnames },
        success: function (data)
        {
            isonloaded(data);
            $("#webchildid").parent().get().innerHTML += CreateTaskHtml(data, parseInt(num.value, 10));
            num.value = parseInt(num.value, 10) + 1;
            jq.ui.hideMask();
        },
        error: function (err) { alert("网络不可用或账号信息过时，请重新登录"); jq.ui.hideMask(); }
    });

}
function CreateTaskHtml(data, num)
{
    var obj = $.parseJSON(data);
    var str = "";
    for (var i = 0, len = obj.length; i < len; i++)
    {
        if (obj[i].FormType === "Html")
        {
            str += "<div ><h2><a style='color:White;' href='AutoFrom.aspx?title=" + obj[i].FlowInstanceCaption + "&OperatorInstanceId=" + obj[i].operatorInstanceId + "' title='" + obj[i].FlowInstanceCaption + "' target='_blank' class='a-title' data-persist-ajax='true' data-refresh-ajax='true' data-pull-scroller='true' >" + subCaption(obj[i].FlowInstanceCaption) + "<span >" + parseInt(parseInt(num * 20) + parseInt(i) + 1) + "</span></a></h2>"
             + "<p>流程编号：" + obj[i].WorkFlowNo + "<br />当前节点：" + obj[i].TaskInstanceCaption + "<br />上一节点：" + obj[i].pOperatedDes + "</p></div>";
        }
        else if (obj[i].FormType === "Page" || obj[i].FormType === "URL")
        {
            str += "<div ><h2><a style='color:White;' href='../Crm/BasePages/WorkTaskCtrlMobilePage.aspx?title=" + obj[i].FlowInstanceCaption + "&OperatorInstanceId=" + obj[i].operatorInstanceId + "' target='_blank'  class='a-title'>" + subCaption(obj[i].FlowInstanceCaption) + "<span >" + parseInt(parseInt(num * 20) + parseInt(i) + 1) + "</span></a></h2>"
                            + "<p>流程编号：" + obj[i].WorkFlowNo + "<br />当前节点：" + obj[i].TaskInstanceCaption + "<br />上一节点：" + obj[i].pOperatedDes + "</p></div>";
        }
        else
        {
            str += "<div ><h2><a style='color:White;' href='javascript:alert(\"未指定跳转页！\"); '  class='a-title'>" + subCaption(obj[i].FlowInstanceCaption) + "<span >" + parseInt(parseInt(num * 20) + parseInt(i) + 1) + "</span></a></h2>"
                + "<p>流程编号：" + obj[i].WorkFlowNo + "<br />当前节点：" + obj[i].TaskInstanceCaption + "<br />上一节点：" + obj[i].pOperatedDes + "</p></div>";

        }
    }
    return str;
}

function subCaption(str)
{
    if (str.length < 20)
        return str + "  >"
    else
        return str.substr(0, 20) + ".. >";

}

function SelectDetp()
{
    var dept = $("#SelectForDept").val();
    var name = $("#selectname").val();
    jq.post("Proc/GetData.ashx", { TYPE: "GetUser", PARA: dept + "," + name }, function (data)
    {
        jq("#testform").html(CreateTaskHtmltest(data));
    });
}

function CreateTaskHtmltest(ud)
{
    var obj = $.parseJSON(ud);
    var str = "";
    for (var i = 0, len = obj.length; i < len; i++)
    {
        str += "<fieldset><input type='checkbox' EmpCode=" + obj[i].EmpCode + " EmpName=" + obj[i].Name + " name='dns_area'  value='" + obj[i].LoginName + "' /> " + obj[i].EmpCode + "--" + obj[i].Name + "--" + obj[i].DeptName + " </fieldset>";
    }
    return str;
}
function SelectDaiban()
{
    $("#jQUi").popup({ title: "任务标题",
        message: "<input type='text' id='formnames' value='" + formname + "' class='jq-ui-forms'>",
        cancelText: "取消",
        cancelCallback: function () { },
        doneText: "查询",
        doneCallback: function ()
        {
            formname = $("#formnames").val();
            LoadDaiban(1);
        },
        cancelOnly: false
    }
                );
}

function showHide(obj, objToHide)
{
    var el = $("#" + objToHide)[0];

    if (obj.className == "expanded")
    {
        obj.className = "collapsed";
    }
    else
    {
        obj.className = "expanded";
    }
    $(el).toggle();

}
function closewindows()
{
    var browsername = navigator.appName;

    if (browsername == "Netscape")
    {
        window.open('', '_self', '');
        window.close();
    }
    else
    {
        if (browsername == "Microsoft Internet Explorer")
        {
            window.opener = "whocares";
            window.opener = null;
            window.open('', '_top');
            window.close();
        }
        else
        {
            try
            {
                window.opener = "whocares";
                window.opener = null;
                window.open('', '_top');
                window.close();
            }
            catch (e) { }
        }
    }

}